//
//  CustomMessageTextCell.m
//  FFMall
//
//  Created by ale tan on 2017/10/10.
//  Copyright © 2017年 IT.Bear. All rights reserved.
//

#import "CustomMessageTextCell.h"
#import "Masonry.h"

@interface CustomMessageTextCell ()
{
    UILabel *mTextLable;
}

@end

@implementation CustomMessageTextCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        mTextLable = [UILabel new];
        mTextLable.numberOfLines = 0;
        mTextLable.backgroundColor = [UIColor clearColor];
        mTextLable.font = APP_FONT_PINGFANG_SC(15);
        [self.contentView addSubview:mTextLable];
        
        if (!isReceived)//是我自己发送的
        {
            [mTextLable mas_makeConstraints:^(MASConstraintMaker *make) {
//                make.top.mas_equalTo(self.contentView.mas_top).offset(TopEdge);
//                make.bottom.mas_equalTo(self.contentView.mas_bottom).offset(-BottomEdge);
                make.centerY.mas_equalTo(self.contentView.mas_centerY);
                make.right.mas_equalTo(headerView.mas_left).offset(-ContentToBubbleHead-HeaderToBubble);
                make.left.mas_greaterThanOrEqualTo(self.contentView.mas_left).offset(ContentToEdge+ContentToBubble);
            }];
            
            [bubbleImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.right.mas_equalTo(mTextLable.mas_right).offset(ContentToBubbleHead);
                make.top.mas_equalTo(mTextLable.mas_top).offset(-ContentToBubble);
                make.bottom.mas_equalTo(mTextLable.mas_bottom).offset(ContentToBubble);
                make.left.mas_equalTo(mTextLable.mas_left).offset(-ContentToBubble);
            }];
            
        }else//别人发送的消息
        {
            [mTextLable mas_makeConstraints:^(MASConstraintMaker *make) {
//                make.top.mas_equalTo(self.contentView.mas_top).offset(TopEdge);
//                make.bottom.mas_equalTo(self.contentView.mas_bottom).offset(-BottomEdge);
               make.centerY.mas_equalTo(self.contentView.mas_centerY); make.left.mas_equalTo(headerView.mas_right).offset(ContentToBubbleHead+HeaderToBubble);
                make.right.mas_lessThanOrEqualTo(self.contentView.mas_right).offset(-ContentToEdge-ContentToBubble);
            }];
            
            [bubbleImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(mTextLable.mas_left).offset(-ContentToBubbleHead);
                make.top.mas_equalTo(mTextLable.mas_top).offset(-ContentToBubble);
                make.bottom.mas_equalTo(mTextLable.mas_bottom).offset(ContentToBubble);
                make.right.mas_equalTo(mTextLable.mas_right).offset(ContentToBubble);
            }];
        }
        
    }
    return self;
}



-(void)setModel:(JMUIChatModel *)model
{
    JMSGCustomContent *content = (JMSGCustomContent *)model.message.content;
//    NSDictionary *dic = content.customDictionary[@"content"];
//    mTextLable.text = dic[@"text"];
    
    mTextLable.text = content.customDictionary[@"content"];
    
    [super setModel:model];
    
//    [self setBubbleImage];
}




-(void)longPress:(UILongPressGestureRecognizer *)Press
{
    if (Press.state == UIGestureRecognizerStateBegan)
    {
        [self becomeFirstResponder];
        
        bubbleImageView.highlighted = YES;
        
        
        UIMenuItem *copy = [[UIMenuItem alloc]initWithTitle:@"复制" action:@selector(menuCopy:)];
        UIMenuItem *retweet = [[UIMenuItem alloc]initWithTitle:@"转发" action:@selector(menuRetweet:)];
        UIMenuItem *retweetMultiple = [[UIMenuItem alloc]initWithTitle:@"转发多条" action:@selector(menuRetweetMultiple:)];
        UIMenuItem *remove = [[UIMenuItem alloc]initWithTitle:@"删除" action:@selector(menuRemove:)];
        
        UIMenuController *menu = [UIMenuController sharedMenuController];
        [menu setMenuItems:@[copy,retweet,retweetMultiple,remove]];
        [menu setTargetRect:bubbleImageView.frame inView:self];
        [menu setMenuVisible:YES animated:YES];
        
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(UIMenuControllerWillHideMenu) name:UIMenuControllerWillHideMenuNotification object:nil];
        
    }
}

/**
 *  @brief  菜单隐藏时调用此方法
 */
-(void)UIMenuControllerWillHideMenu
{
    bubbleImageView.highlighted = NO;
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


-(BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
    return  ((action == @selector(menuCopy:))   || (action == @selector(menuRemove:))  ||
             (action == @selector(menuRetweet:)) || (action == @selector(menuRetweetMultiple:)));
}


#pragma mark --复制、删除、转发、转发多条
-(void)menuCopy:(id)sender
{
    [UIPasteboard generalPasteboard].string = mTextLable.text;
    
}


-(void)menuRetweet:(id)sender
{
    
}

-(void)menuRetweetMultiple:(id)sender
{
    
}

-(void)menuRemove:(id)sender
{
    [self routerEventWithType:EventChatCellRemoveEvent userInfo:@{kModelKey:self.model}];
}

@end
